﻿using UnityEngine;
using System.Collections;

public class CameraMovCtrl : MonoBehaviour {

	public bool IsDragging
	{
		get{
			return dragging;
		}
	}

	float speed;
	bool dragging;
	Vector3 lastFrameInput;

	public UserInputManager inputmgr;

	// Use this for initialization
	void Start () {
		speed = 0.05f;
		inputmgr.evthMouseInput += UserInput;
	}
	
	// Update is called once per frame
	void Update () {

	}

	void UserInput(object sender, EventMouseInput input)
	{
		if (input.eTarget != ESelTarget.emSelCam)
			return;

		if (Input.GetMouseButton (0)){
			// test if try dragging
			if(!dragging)
			{
				Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
				
				RaycastHit hit;
				
				if(Physics.Raycast(ray, out hit))
				{
					if(hit.collider.gameObject.tag == "Ground")
					{
						dragging = true;
						lastFrameInput = Input.mousePosition;
					}
				}
			}
			
			if(dragging)
			{
				Vector3 offset = Input.mousePosition - lastFrameInput;
				lastFrameInput = Input.mousePosition;

				transform.position -= new Vector3(offset.x * speed, 0, offset.y * speed);
			}
		}
		
		if (Input.GetMouseButtonUp (0)) {
			dragging = false;
		}
	}
}